function setCtx(ctx) {
	this.ctx = ctx;
	
}

$(function (){
	var Stars1 = new Stars("stars1",{nowClass:"current-rating",tipsTxt:["1星 - 轻微","2星 - 不是很严重","3星- 有点严重","4星 - 严重","5星 - 非常严重"]});
})


function checkTitle(){
	var title = $("#title").val();
	if(title == null || title == ""){
		layer.tips('标题不能为空!', '#title', {
			tips: 3,
			time:1000
		});
		return false;
	} else {
		return true;
	}
}


function checkContactor(){
	var contactor = $("#contactor").val();
	if(contactor == null || contactor == ""){
		layer.tips('请填写联系人!', '#contactor', {
			tips: 3,
			time:1000
		});
		return false;
	} else{
		return true;
	}
}

function checkContactorPhone(){
	var contactorPhone = $("#contactorPhone").val();
	if(contactorPhone == $("#contactorPhone").attr("placeholder")){
		contactorPhone = "";
	}
	if(contactorPhone == null || contactorPhone == ""){
		layer.tips('请填写联系电话!', '#contactorPhone', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		var reg = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|15[0-9]|18[8|9])\d{8}$)/;
		if (!reg.test(contactorPhone)) {
			layer.tips('电话号码格式错误，可填固定电话或手机', '#contactorPhone', {
				tips : 3
			});
			return false;
		} else {
			return true;
		}
	}
}
	
function checkContent(){
	var content = $("#content").val();
	if(content == null || content == ""){
		layer.tips('请填写问题描述!', '#content', {
			tips: 3,
			time:1000
		});
		return false;
	} else{
		return true;
	}
}

function checkLevel(){
	var level = $("#level").val();
	if(level == null || level == ""){
		layer.tips('请选择危害程度!', '#stars1', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

function checkType(){
	var type = $("#type").val();
	if(type == null || type == "" || type == "-1"){
		layer.tips('请选择类型!', '#type', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

function checkPhoto(){
	var imgLength = $("#imgLength").val();
	if(imgLength == null || imgLength == ""){
		layer.tips('请上传照片,最多五张！!', '#imgArea', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

function showdelete(target){
	$(target).children("span").css("display","block");
}

function hidedelete(target){
	$(target).children("span").css("display","none");
}


//保存或新增方法
var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
function saveOrUpdate() {
	
	//申诉类别
	var suggestionType;
	var temp = document.getElementsByName("compla");
	for (var i = 0; i < temp.length; i++) {
		if (temp[i].checked) {
			var suggestionType = temp[i].value; // 这里获取选中的radio的value值
		}
	}
	
	
	var objId = $("#shopId").val(); 
	var complainType;
	
	if(objId == null || objId == "") {
		complainType = 0; //投诉平台
	} else {
		complainType = 1; //投诉商家
	}
	
	var title = $("#title").val();
	var houseId = $("#houseId").val();
	var contactor = $("#contactor").val();
	var contactorPhone = $("#contactorPhone") .val();
	var content = $("#content").val();
	var level = $("#level").val();
	var type = $("#type").val();
	var length = $("#imgLength").val();
	var imgArr = [];
	
	
	for(var i=1;i <= length;i++){
		if($("#imgId" + i).val() != null){
			imgArr.push($("#imgId" + i).val());
		}
	}
	
	if (checkTitle() && checkContactor() && checkContactorPhone() && 
			checkContent() && checkLevel() && checkType() && checkPhoto()) {
		var index1 = layer.load(1);
		$.post(ctx + "/orderInfo/saveComplain", {
			imgArr:imgArr, 
			title : title,
			suggestionType:suggestionType,
			complainType:complainType,
			houseId:houseId,
			contactor:contactor,
			contactorPhone:contactorPhone,
			level:level,
			type:type,
			content:content,
			objId:objId
			},
		function(data) {
			data = JSON.parse(data);
			if (data.success) {
				layer.msg("操作成功", {
					icon : 1,
					time : 1000
				// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					layer.close(index1);
					parent.window.location.href = ctx
							+ "/orderInfo/orderInfoListByState";
					parent.layer.close(index); // 根据窗口索引关闭窗口

				});
			} else {
				layer.msg(data.msg, {
					icon : 2,
					time : 1000
				// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					layer.close(index1);
					parent.layer.close(index); // 根据窗口索引关闭窗口
				});
			}
		});
	}
}


	var Class = {
	    create: function() {
	        return function() { this.initialize.apply(this, arguments); }
	    }
	}
	var Extend = function(destination, source) {
	    for (var property in source) {
	        destination[property] = source[property];
	    }
	}
	function stopDefault( e ) {
	     if ( e && e.preventDefault ){
	        e.preventDefault();
	    }else{
	        window.event.returnValue = false;
	    }
	    return false;
	} 
	/**
	 * 星星打分组件
	 */
	var Stars = Class.create();
	var score;
	Stars.prototype = {
	    initialize: function(star,options) {
	        this.SetOptions(options); //默认属性
	        var flag = 999; //定义全局指针
	        var isIE = (document.all) ? true : false; //IE?
	        var starlist = document.getElementById(star).getElementsByTagName('a'); //星星列表
	        var input = document.getElementById(this.options.Input) || document.getElementById(star+"-input"); // 输出结果
	       
	        var tips = document.getElementById(this.options.Tips) || document.getElementById(star+"-tips"); // 打印提示
	        var nowClass = " " + this.options.nowClass; // 定义选中星星样式名
	        var tipsTxt = this.options.tipsTxt; // 定义提示文案
	        var len = starlist.length; //星星数量
	        var tempValue = "";
	       
	        for(i=0;i<len;i++){ // 绑定事件 点击 鼠标滑过
	            starlist[i].value = i;
	            starlist[i].onclick = function(e){
	                stopDefault(e);
	                this.className = this.className + nowClass;
	                flag = this.value;
	                input.value = this.getAttribute("star:value");
	                tips.innerHTML = tipsTxt[this.value];
	                tempValue = tipsTxt[this.value];
	                $("#level").val(input.value);
	            }
	            starlist[i].onmouseover = function(){
	                if (flag < 999){
	                    var reg = RegExp(nowClass,"g");
	                    tips.innerHTML = tipsTxt[this.value];
	                    starlist[flag].className = starlist[flag].className.replace(reg,"");
	                    //tips.innerHTML = tipsTxt[this.value]
	                }
	            }
	            starlist[i].onmouseout = function(){
	                if (flag < 999){
	                    starlist[flag].className = starlist[flag].className + nowClass;
	                }
	                tips.innerHTML = tempValue;
	            }
	        };
	        if (isIE){ //FIX IE下样式错误
	            var li = document.getElementById(star).getElementsByTagName('li');
	            for (var i = 0, len = li.length; i < len; i++) {
	                var c = li[i];
	                if (c) {
	                    c.className = c.getElementsByTagName('a')[0].className;
	                }
	            }
	        }
	    },
	    //设置默认属性
	    SetOptions: function(options) {
	        this.options = {//默认值
	            Input:      "",//设置触保存分数的INPUT
	            Tips:       "",//设置提示文案容器
	            nowClass:   "current-rating",//选中的样式名
	            tipsTxt:    ["1星 - 轻微","2星 - 不是很严重","3星- 有点严重","4星 - 严重","5星 - 非常严重"]//提示文案
	        };
	        Extend(this.options, options || {});
	    }
	}
	
	